<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" class="formWidth" label-width="100px">
      <el-form-item label="伤残性质" prop="disabledProperty">
        <el-select v-model="queryParams.disabledProperty" placeholder="请选择伤残性质" clearable size="small">
          <el-option
            v-for="dict in disabledPropertyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="伤残等级" prop="disabledLevel">
        <el-select v-model="queryParams.disabledLevel" placeholder="请选择伤残等级" clearable size="small">
          <el-option
            v-for="dict in disabledLevelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="残疾证件号码" prop="disabledCertificateCode">
        <el-input
          v-model="queryParams.disabledCertificateCode"
          placeholder="请输入残疾证件号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评残时间
" prop="evaluationDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.evaluationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择评残时间
">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="孤老情况" prop="isLonely">
        <el-select v-model="queryParams.isLonely" placeholder="请选择孤老情况" clearable size="small">
          <el-option
            v-for="dict in isLonelyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供养情况" prop="supportSituation">
        <el-select v-model="queryParams.supportSituation" placeholder="请选择供养情况" clearable size="small">
          <el-option
            v-for="dict in supportSituationOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供养单位" prop="supportUnit">
        <el-select v-model="queryParams.supportUnit" placeholder="请选择供养单位" clearable size="small">
          <el-option
            v-for="dict in supportUnitOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="原服务单位" prop="originalServiceUnit">
        <el-input
          v-model="queryParams.originalServiceUnit"
          placeholder="请输入原服务单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="现就业状况" prop="employmentSituation">
        <el-select v-model="queryParams.employmentSituation" placeholder="请选择现就业状况" clearable size="small">
          <el-option
            v-for="dict in employmentSituationOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['archive_info:disabled_militiaman:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['archive_info:disabled_militiaman:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['archive_info:disabled_militiaman:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['archive_info:disabled_militiaman:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="disabled_militiamanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="soldierName" :show-overflow-tooltip="true"/>
      <el-table-column label="身份证号" :show-overflow-tooltip="true" align="center" prop="idCard"  width="190" :formatter="idNumberFormat"/>
      <el-table-column label="现就业状况" align="center" prop="id" :show-overflow-tooltip="true"/>
      <el-table-column label="伤残性质" align="center" prop="disabledProperty" :formatter="disabledPropertyFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="伤残等级" align="center" prop="disabledLevel" :formatter="disabledLevelFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="残疾证件号码" align="center" prop="disabledCertificateCode" :show-overflow-tooltip="true"/>
      <el-table-column label="评残时间" align="center" prop="evaluationDate" width="180" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.evaluationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="孤老情况" align="center" prop="isLonely" :formatter="isLonelyFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="供养情况" align="center" prop="supportSituation" :formatter="supportSituationFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="供养单位" align="center" prop="supportUnit" :formatter="supportUnitFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="原服务单位" align="center" prop="originalServiceUnit" :show-overflow-tooltip="true"/>
      <el-table-column label="现就业状况" align="center" prop="employmentSituation" :formatter="employmentSituationFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['archive_info:disabled_militiaman:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['archive_info:disabled_militiaman:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改伤残民兵民工信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" class="formWidth" label-width="140px">
        <el-form-item label="伤残性质" prop="disabledProperty">
          <el-select v-model="form.disabledProperty" placeholder="请选择伤残性质">
            <el-option
              v-for="dict in disabledPropertyOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="伤残等级" prop="disabledLevel">
          <el-select v-model="form.disabledLevel" placeholder="请选择伤残等级">
            <el-option
              v-for="dict in disabledLevelOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="残疾证件号码" prop="disabledCertificateCode">
          <el-input v-model="form.disabledCertificateCode" placeholder="请输入残疾证件号码" />
        </el-form-item>
        <el-form-item label="评残时间" prop="evaluationDate">
          <el-date-picker clearable size="small"
            v-model="form.evaluationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择评残时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="孤老情况" prop="isLonely">
          <el-select v-model="form.isLonely" placeholder="请选择孤老情况">
            <el-option
              v-for="dict in isLonelyOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供养情况" prop="supportSituation">
          <el-select v-model="form.supportSituation" placeholder="请选择供养情况">
            <el-option
              v-for="dict in supportSituationOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供养单位" prop="supportUnit">
          <el-select v-model="form.supportUnit" placeholder="请选择供养单位">
            <el-option
              v-for="dict in supportUnitOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原服务单位" prop="originalServiceUnit">
          <el-input v-model="form.originalServiceUnit" placeholder="请输入原服务单位" />
        </el-form-item>
        <el-form-item label="现就业状况" prop="employmentSituation">
          <el-select v-model="form.employmentSituation" placeholder="请选择现就业状况">
            <el-option
              v-for="dict in employmentSituationOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDisabled_militiaman, getDisabled_militiaman, delDisabled_militiaman, addDisabled_militiaman, updateDisabled_militiaman, exportDisabled_militiaman } from "@/api/archive_info/disabled_militiaman";

export default {
  name: "Disabled_militiaman",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 伤残民兵民工信息表格数据
      disabled_militiamanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 伤残性质字典
      disabledPropertyOptions: [],
      // 伤残等级字典
      disabledLevelOptions: [],
      // 孤老情况字典
      isLonelyOptions: [],
      // 供养情况字典
      supportSituationOptions: [],
      // 供养单位字典
      supportUnitOptions: [],
      // 现就业状况字典
      employmentSituationOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        disabledProperty: null,
        disabledLevel: null,
        disabledCertificateCode: null,
        evaluationDate: null,
        isLonely: null,
        supportSituation: null,
        supportUnit: null,
        originalServiceUnit: null,
        employmentSituation: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        disabledProperty: [
          { required: true, message: "伤残性质不能为空", trigger: "change" }
        ],
        disabledLevel: [
          { required: true, message: "伤残等级不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("disability_property").then(response => {
      this.disabledPropertyOptions = response.data;
    });
    this.getDicts("disability_level").then(response => {
      this.disabledLevelOptions = response.data;
    });
    this.getDicts("yes_or_no").then(response => {
      this.isLonelyOptions = response.data;
    });
    this.getDicts("yes_or_no").then(response => {
      this.supportSituationOptions = response.data;
    });
    this.getDicts("yes_or_no").then(response => {
      this.supportUnitOptions = response.data;
    });
    this.getDicts("employment_situation").then(response => {
      this.employmentSituationOptions = response.data;
    });
  },
  methods: {
    /** 查询伤残民兵民工信息列表 */
    getList() {
      this.loading = true;
      listDisabled_militiaman(this.queryParams).then(response => {
        this.disabled_militiamanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 伤残性质字典翻译
    disabledPropertyFormat(row, column) {
      return this.selectDictLabel(this.disabledPropertyOptions, row.disabledProperty);
    },
    // 伤残等级字典翻译
    disabledLevelFormat(row, column) {
      return this.selectDictLabel(this.disabledLevelOptions, row.disabledLevel);
    },
    // 孤老情况字典翻译
    isLonelyFormat(row, column) {
      return this.selectDictLabel(this.isLonelyOptions, row.isLonely);
    },
    // 供养情况字典翻译
    supportSituationFormat(row, column) {
      return this.selectDictLabel(this.supportSituationOptions, row.supportSituation);
    },
    // 供养单位字典翻译
    supportUnitFormat(row, column) {
      return this.selectDictLabel(this.supportUnitOptions, row.supportUnit);
    },
    // 现就业状况字典翻译
    employmentSituationFormat(row, column) {
      return this.selectDictLabel(this.employmentSituationOptions, row.employmentSituation);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        soldierId: null,
        disabledProperty: null,
        disabledLevel: null,
        disabledCertificate: null,
        disabledCertificateCode: null,
        evaluationDate: null,
        isLonely: null,
        supportSituation: null,
        supportUnit: null,
        originalServiceUnit: null,
        employmentSituation: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加伤残民兵民工信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDisabled_militiaman(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改伤残民兵民工信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDisabled_militiaman(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDisabled_militiaman(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除伤残民兵民工信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDisabled_militiaman(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有伤残民兵民工信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDisabled_militiaman(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    idNumberFormat(row,column){
      return row.idNumber.replace(/^(.{6})(?:\w+)(.{4})$/, "\$1********\$2")
    },
  }
};
</script>
